﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Du_Lich
{
    public partial class KeHoachTourForm : Form
    {
        public KeHoachTourForm()
        {
            InitializeComponent();
        }

        string StrConn = ConfigurationManager.ConnectionStrings["DuLich"].ConnectionString;
        SqlConnection conn = null;
        SqlCommand cmd = null;
        SqlDataAdapter da = null;

        public void MoKetNoi()
        {
            conn = new SqlConnection(StrConn);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
        }

        public void DongKetNoi()
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }

        public DataTable LoadTour()
        {
            string strLoad = "select MaTour,TenTour,DiemKhoiHanh,DiemDen1,DiemDen2,DiemKetThuc,KhachSan1,KhachSan2,KhachSan3,GiaTour,TongNgay from TOUR";
            conn = new SqlConnection(StrConn);
            da = new SqlDataAdapter(strLoad, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

        private int TimKiem()
        {
            string TimMa = txtMaTour.Text;
            int i = -1;
            foreach (DataGridViewRow dgv in gvTour.Rows)
            {
                try
                {
                    i++;
                    if (string.Compare(TimMa, dgv.Cells[0].Value.ToString()) == 0)
                        return i;
                }
                catch (Exception)
                {
                    MessageBox.Show("Ban nhap sai roi ! Nhap lai di");
                }
            }
            return -1;
        }

        public void Xoa(string matour)
        {
            string strDelete = "Delete TOUR where MaTour=@matour";
            cmd = new SqlCommand(strDelete, conn);
            cmd.Parameters.AddWithValue("@matour", matour);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        public void Them(string matour,string tentour,string dkh,string dd1,string dd2,string dkt,string gt,string ks1,string ks2,string ks3,int tn)
        {
            string strInsert = "Insert TOUR (MaTour,TenTour,DiemKhoiHanh,DiemDen1,DiemDen2,DiemKetThuc,GiaTour,KhachSan1,KhachSan2,KhachSan3,TongNgay) values(@matour,@tentour,@dkh,@dd1,@dd2,@dkt,@gt,@ks1,@ks2,@ks3,@tn)";
            cmd = new SqlCommand(strInsert, conn);
            cmd.Parameters.AddWithValue("@matour", matour);
            cmd.Parameters.AddWithValue("@tentour", tentour);
            cmd.Parameters.AddWithValue("@dkh", dkh);
            cmd.Parameters.AddWithValue("@dd1", dd1);
            cmd.Parameters.AddWithValue("@dd2", dd2);
            cmd.Parameters.AddWithValue("@dkt", dkt);
            cmd.Parameters.AddWithValue("@gt", gt);
            cmd.Parameters.AddWithValue("@ks1", ks1);
            cmd.Parameters.AddWithValue("@ks2", ks2);
            cmd.Parameters.AddWithValue("@ks3", ks3);
            cmd.Parameters.AddWithValue("@tn", tn);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        private void KeHoachTourForm_Load(object sender, EventArgs e)
        {
            gvTour.Columns.Clear();

            for (int i = 1; i < 11; i++)
                cbtongngay.Items.Add(i);
            MoKetNoi();
            gvTour.DataSource = LoadTour();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Them(txtMaTour.Text, txtTenTour.Text, txtDiemKhoiHanh.Text, txtDiemDen1.Text, txtDiemDen2.Text, txtDiemKetThuc.Text, txtGiaTour.Text, txtKhachSan1.Text, txtKhachSan2.Text, txtKhachSan3.Text, int.Parse(cbtongngay.Text));
            gvTour.DataSource = LoadTour();
        }

        private void KeHoachTourForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DongKetNoi();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {       
            if (gvTour.Rows.Count == 0)
                return;
            int i = gvTour.SelectedRows[0].Index;
            Xoa(gvTour.Rows[i].Cells[0].Value.ToString());
            gvTour.DataSource = LoadTour();
            
        }

        private void gvTour_SelectionChanged(object sender, EventArgs e)
        {
            if (gvTour.SelectedRows.Count == 0)
                return;
            int i = gvTour.SelectedRows[0].Index;
            txtMaTour.Text = gvTour.Rows[i].Cells[0].Value.ToString();
            txtGiaTour.Text = gvTour.Rows[i].Cells[9].Value.ToString();
            cbtongngay.Text = gvTour.Rows[i].Cells[10].Value.ToString();
            txtTenTour.Text = gvTour.Rows[i].Cells[1].Value.ToString();
            txtDiemKhoiHanh.Text = gvTour.Rows[i].Cells[2].Value.ToString();
            txtDiemDen1.Text = gvTour.Rows[i].Cells[3].Value.ToString();
            txtDiemDen2.Text = gvTour.Rows[i].Cells[4].Value.ToString();
            txtDiemKetThuc.Text = gvTour.Rows[i].Cells[5].Value.ToString();
            txtKhachSan1.Text = gvTour.Rows[i].Cells[6].Value.ToString();
            txtKhachSan2.Text = gvTour.Rows[i].Cells[7].Value.ToString();
            txtKhachSan3.Text = gvTour.Rows[i].Cells[8].Value.ToString();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            int kq = TimKiem();
            if (kq == -1)
                MessageBox.Show("Khong tim thay", "Thong bao tim kiem");
            else
                gvTour.Rows[kq].Selected = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DongKetNoi();
            this.Close();
                
        }

    }
}
